
#pragma once

#include "../../CommonLib/src/StopWatch.h"

namespace FlameEngine
{
	struct GameTime
	{
	public:
		GameTime() :m_elapsedTime(0),
			m_elapsedLogic(0),
			m_totalLogic(0),
			m_totalTime(0)
		{
		}

		double GetElapsed() { return m_elapsedTime; }
		double GetTotal() { return m_totalTime; }
		double GetElapsedLogicTime() { return m_elapsedLogic; }
		double GetTotalLogicTime() { return m_totalLogic; }

		void SetTime_(double elapsedTime, double totalTime,double elapsedLogic,double totalLogic)
		{
			m_elapsedTime = elapsedTime;
			m_totalTime = totalTime;
			m_elapsedLogic = elapsedLogic;
			m_totalLogic = totalLogic;
		}

		void ResetElapsedTime()
		{
			m_elapsedTime = 0;
			m_elapsedLogic = 0;
		}
	private:
		double m_elapsedTime;
		double m_totalTime;
		double m_elapsedLogic;
		double m_totalLogic;
	};

	class GameTimer
	{
	public:
		GameTimer();

		//Get the total time since program start in seconds
		double GetTotalTime()const {return m_totalTime;}

		//Get elapsed time since last update
		double GetElapsedTime() const {return m_elapsedTime;}

		void SetPause(bool isPaused){m_isPaused = isPaused;}

		bool IsPaused()const {return m_isPaused;}

		void SetTimeScale(float scale){m_timeScale = scale;}
		double GetTimeScale()const { return m_timeScale; }

		//update game time once, only work when time is paused.
		void SingleStep();
		
		//Adjust the elapsed time to a reasonable value if it's too larger.
		//Useful in debugging, so you can set a break point,stop for a while but still get
		//smooth update elapsed time.
		void EnableDebugAdjust(bool enable){m_enableDebugAdjust = enable;}


		//Internal method, start timer,call bye game engine at app start up.
		void Start_();

		//Internal method,update timer,called by game engine at the very beginning of each game loop
		void Update_();
	private:
		double m_elapsedTime;
		double m_totalTime;
		double m_timeScale;
		double m_lastUpdateTime;
		StopWatch m_stopWatch;
		
		bool m_enableDebugAdjust;
		bool m_isPaused;

		GameTimer(const GameTimer&);
		GameTimer& operator=(const GameTimer);
	};
}